// components/detailPage/index.js
import {
  ApiItemDetail,
  ApiGetmyCoupon
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: {},
    isActive: false,
    warning: [],
    active: false,
    activeNames: [],
    table_sl: 'sec',
    line_sl: 'all',
    headList: [
      '服务场景',
      ['', '退费规则'],
    ],
    list: [
      ['开始服务后订单不可取消', '不予退费', ],
      ['服务人员出门前取消订单', '全额原路退回'],
      ['服务人员已出门未服务', '交通费不退,服务费扣除20%'],
      ['服务人员已上门未服务', '交通费不退,服务费扣除50%'],
      ['服务人员现场评估无法服务', '交通费不退,服务费扣除50%'],
    ],
  },



  handleCoupon(e) {
    wx.navigateTo({
      url: `/pages/costDetailsCoupon/index`,
    })
  },

  getCouponList() {
    ApiGetmyCoupon({
      coupon_type: '可用'
    }).then(res => {
      wx.setStorageSync('counponArr', res.data)
      this.setData({
        counponArr: res.data,
      })
    })
  },


  serviceAgreement() {
    wx.navigateTo({
      url: '/components/serviceAgreement/index',
    })
  },
  userInformedNotice() {
    wx.navigateTo({
      url: '/components/userInformedNotice/index',
    })
  },
  //切换耗材费
  materialPriceChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id
    this.getDataList(id)
  },
  extentClick(e) {
    this.data.dataList.extend_price[e.currentTarget.dataset.index].isActive = !this.data.dataList.extend_price[e.currentTarget.dataset.index].isActive
    this.setData({
      dataList: this.data.dataList
    })

  },
  onChange(e) {
    this.setData({
      active: !this.data.active
    })
  },
  getDataList(val) {
    ApiItemDetail(val).then(res => {
      //价格详情
      if (res.data.price_detail && res.data.price_detail.length > 0) {
        res.data.price_detailCopy = this.forMatterPrice(res.data.price_detail)
      }
      //耗材费
      if (res.data.material_price_detail && res.data.material_price_detail.length > 0) {
        res.data.material_price_detailCopy = this.forMatterPrice(res.data.material_price_detail)
      }

      if (res.data.warning) {
        this.setData({
          warning: JSON.parse(res.data.warning)
        })
      }

      this.setData({
        dataList: res.data
      })
    })
  },
  forMatterPrice(price_detail) {
    let val = JSON.parse(price_detail);
    let data = [];
    for (const key in val) {
      data.push({
        name: key,
        value: val[key]
      });
    }
    return data
  },

  //预约服务
  handleFile() {
    if (this.isRealName()) {
      if (this.data.active) {
        let extend_price = []
        this.data.dataList.extend_price.forEach(item => {
          if (item.isActive) {
            extend_price.push(item.id)
          }
        })
        let query = {
          allow_refund: this.data.dataList.allow_refund,
          id: this.data.dataList.id,
          patient_pic_required: this.data.dataList.patient_pic_required,
          patient_status_required: this.data.dataList.patient_status_required,
          extend_price: extend_price,
          coupon_id: this.data.dataRadio?.id
        }
        wx.navigateTo({
          url: `/pages/orderInsert/index?dataList=${JSON.stringify(query)}`
        })
      } else {
        wx.showModal({
          title: "请阅读并勾选《用户须知》和《用户知情告知》！",
          icon: 'error',
          showCancel: false
        })
      }
    }
  },

  //判断是否实名认证
  isRealName() {
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo.card_no && userInfo.card_no.length > 0) {
      return true
    } else {
      wx.showModal({
        title: '提示',
        content: '您还未实名认证，是否立即前往？',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/realName/index?data=""',
            })
          }
        }
      })
      return false
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCouponList()
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let dataRadio = currPage.data?.dataRadio
    if (dataRadio && dataRadio.id != '0') {
      this.data.dataList.price_detailCopy.forEach((item, index) => {
        //计算包不包含服务费
        if (item.name.includes('服务费')) {
          console.log(item)
          if (Number(this.data.dataRadio.coupon_price) > Number(item.value)) {
            this.setData({
              "dataRadio.coupon_price": item.value,
            })
          }
        }
      })
      this.setData({
        dataRadio
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})